<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>筛选、替换并展示表格</title>
</head>

<body>
    <!--载入默认美化样式-->
    <link rel="stylesheet" href="https://unpkg.com/@picocss/pico@1.*/css/pico.min.css">
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <div id="Application">

        <select v-model="selectedId">
            <option v-for="(item,index) in arrId" :key="index" :value="item">{{item}}</option>
        </select>
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>计划</th>
                    <th>时间</th>
                    <th>展现次数</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(item,index) in filteredData" :key="item.index">
                    <td>{{ item.id }}</td>
                    <td>{{ item.name }}</td>
                    <td>{{ item.date }}</td>
                    <td>{{ item.count }}</td>
                </tr>
            </tbody>
        </table>

    </div>
    <script>
        const App = Vue.createApp({
            setup() {


                //原始数据数组
                const dataList = Vue.reactive([
                    { id: '2', date: '2023-04-20', count: '1' },
                    { id: '3', date: '2023-04-20', count: '1' },
                    { id: '4', date: '2023-04-20', count: '1' },
                    { id: '2', date: '2023-04-20', count: '1' }
                ]);
                //替换数组
                const replaceId = [
                    { id: '2', name: "五一投放图片展现广告" },
                    { id: '2199', name: "五一投放图片点击广告" }
                ];

                //选择的结果值
                const selectedId = Vue.ref('All');
                //展示的筛选值
                const filteredData = Vue.computed(() => {

                    if (selectedId.value === 'All') {
                        const data = replace(dataList, replaceId);
                        return data;
                    } else {
                        const arr = dataList.filter(item => item.id === selectedId.value);
                        const data = replace(arr, replaceId);
                        return data;
                    }
                });

                //拿到ID组成选项数组
                const arrIdFn = () => {
                    // 用 map 方法获取 ID 数组
                    const idList = dataList.map(item => item.id);

                    // 用 Set 数据结构进行去重
                    const uniqueIdList = [...new Set(idList)];

                    // 按从小到大的顺序排序
                    uniqueIdList.sort((a, b) => a - b);
                    //插入all
                    uniqueIdList.unshift("All");
                    return uniqueIdList;
                }
                const arrId = Vue.ref();
                arrId.value = arrIdFn();


                //添加ID对应计划关系
                const replace = (data, replaceId) => {
                    const a = data;
                    const b = replaceId;
                    const bMap = new Map(b.map(item => [item.id, item.name]));
                    a.forEach(item => {
                        item.name = bMap.get(item.id) || '没有计划';
                    });
                    return a;
                }


                return {
                    selectedId,
                    filteredData,
                    arrId,

                };

            },
            //template: ``
        });
        App.mount("#Application");
    </script>
</body>

</html>